<template>
	<view class="container">
		<view class="header">
			<view class="step active">
				<view class="step-num">
					1
				</view>
				<view class="step-text">
					填写任务需求
				</view>
			</view>
			<view class="step-arrow active">
				<view class="icon-arrow">
					<image src="../../../static/images/arrow-half.png" mode=""></image>
				</view>
			</view>
			<view class="step active">
				<view class="step-num">
					2
				</view>
				<view class="step-text">
					设置任务详情
				</view>
			</view>
		</view>
		<view class="main">
			<view class="taskCode">
				<textarea placeholder="请添加任务口令或者任务链接" v-model="formData.content" @input="input" />
				<view class="tips" v-if="tipShow">
					查看教程
				</view>
			</view>
			<view class="taskVerifyImg">
				<view class="verifyTitle">
					添加验证图
				</view>
				<view class="tips">
					<view class="text">
						下图为系统自动为您匹配的验证图
					</view>
					<view class="text">
						您也可以点击 <text>「更换验证图」</text>换成自己的验证图
					</view>
				</view>
				<view class="verifyImage">
					<view class="verifyUpload" @click="handleUpload">
						<text class="iconfont iconxiangji-tianchong"></text> <text class="text">更换验证图</text>
					</view>
					<view class="verifyResult">
						<image :src="defaultImage" mode=""></image>
					</view>
				</view>
			</view>
		</view>
		<view class="stepBtnWrapper">
			<view class="stepBtn" @click="handleStep">
				发布任务
			</view>
		</view>
		
	</view>
</template>

<script>
const form = require("@/components/common/tui-validation/tui-validation.js")
import {publishTask} from '@/http/api.js'
	export default {
		components: {
	
		},
		data() {
			return {
				defaultImage: '../../../static/images/19.jpg',
				tipShow: true,
				formData: {}
			}
		},
		onLoad(e) {
			console.log(e)
			this.formData = JSON.parse(e.formData) 
		},
		onReady() {
		},
		methods: {
			input (e) {
				console.log(e)
				let value = e.target.value
				if (value !== '') {
					this.tipShow = false
				} else {
					this.tipShow = true
				}
				
			},
			handleStep () {
				let rules = [{
					name: "content",
					rule: ["required"], //可使用区间，此处主要测试功能
					msg: ["请添加你的口令"]
				}];
				let checkRes = form.validation(this.formData, rules);
				console.log(this.formData)
				if (!checkRes) {
					publishTask(this.formData).then(resp => {
						if (resp.code === '10000') {
							uni.navigateTo({
								url:"/pages/task/pdd/success"
							})
						}
					})
					
				} else {
					uni.showToast({
						title: checkRes,
						icon: "none"
					});
				}
			},
			handleUpload () {
				uni.chooseImage({
				    count: 1, //默认9
				    sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
				    sourceType: ['album', 'camera'], //从相册选择
				    success: (chooseImageRes) => {
							const tempFilePaths = chooseImageRes.tempFilePaths
				        // console.log(JSON.stringify(chooseImageRes.tempFilePaths));
								this.defaultImage = tempFilePaths[0]
								uni.uploadFile({
									url: 'https://api.yanglao12349.com/upload', //仅为示例，非真实的接口地址
									filePath: tempFilePaths[0],
									name: 'file',
									formData: {
											'user': 'test'
									},
									success: (uploadFileRes) => {
											console.log(uploadFileRes.data);
									}
							});
				    }
				});
			}
		}
	}
</script>
<style>
	page {
		background: #F8F8F8;
	}
</style>
<style scoped lang="scss">
	.container {
		.header {
			position: fixed;
			left: 0;
			top: 0;
			width: 100%;
			height: 90rpx;
			padding: 0 110rpx;
			line-height: 90rpx;
			box-sizing: border-box;
			background: linear-gradient(#fc4250, #fe744b);
			z-index: 1;
			display: flex;
			.step {
				display: flex;
				align-items: center;
				opacity: 0.5;
				flex: 1;
				justify-content: center;
				&.active {
					opacity: 1;
				}
				.step-num {
					flex: 0 0 32rpx;
					width: 32rpx;
					height: 32rpx;
					margin-right: 15rpx;
					line-height: 32rpx;
					border-radius: 50%;
					background: #fff;
					font-size: 16rpx;
					color: #e05f59;
					text-align: center;
				}
				.step-text {
					font-size: 22rpx;
					color: #fff;
				}
			}
			.step-arrow {
				display: flex;
				height: 100%;
				align-items: center;
				opacity: 0.5;
				flex:0 0 98rpx;
				&.active {
					opacity: 1;
				}
				.icon-arrow {
					width: 48rpx;
					height: 18rpx;
					margin: 0 auto;
					image {
						display: block;
						width: 100%;
						height: 18rpx;
					}
				}
			}
		}

		.main {
			position: relative;
			z-index: 0;
			margin-top: 90rpx;
			padding:20rpx 20rpx 150rpx;
			box-sizing: border-box;
			.taskCode {
				position: relative;
				width: 100%;
				height: 270rpx;
				padding: 40rpx;
				margin-bottom: 20rpx;
				border-radius: 10rpx;
				box-sizing: border-box;
				background: #fff;
				textarea {
					width: 100%;
					height: 100%;
					font-size: 26rpx;
					color: #333;
				}
				.tips {
					position: absolute;
					top: 40rpx;
					right: 40rpx;
					font-size: 26rpx;
					color: #cd2834;
				}
			}
			.taskVerifyImg {
				padding: 40rpx;
				border-radius: 10rpx;
				background: #fff;
				.verifyTitle {
					margin-bottom: 22rpx;
					font-weight: bold;
					font-size: 28rpx;
					text-align: center;
					color: #333;
				}
				.tips {
					margin-bottom: 40rpx;
					word-spacing: 3rpx;
					letter-spacing: 3rpx;
					text-align: center;
					font-size: 24rpx;
					color: #adadad;
					text {
						font-weight: bold;
						color: #f72c5d;
					}
				}
				.verifyImage {
					width: 315rpx;
					height: 586rpx;
					margin: 0 auto;
					.verifyUpload {
						width: 100%;
						height: 70rpx;
						line-height: 70rpx;
						border-radius: 10rpx 10rpx 0 0;
						background: #fc4250;
						text-align: center;
						.iconfont {
							vertical-align: middle;
							font-size: 35rpx;
							color: #fff;
						}
						.text {
							padding-left: 15rpx;
							font-size: 24rpx;
							color: #fff;
							vertical-align: middle;
						}
					}
					.verifyResult {
						width: 100%;
						height: calc(100% - 70rpx);
						overflow: hidden;
						border-radius: 0rpx 0rpx 10rpx 10rpx;
						image {
							width: 100%;
							min-height: 100%;
						}
					}
				}
			}
		}
		.stepBtnWrapper {
			position: fixed;
			bottom: 0rpx;
			left: 50%;
			width: 664rpx;
			height: 120rpx;
			transform: translateX(-50%);
			background:#F8F8F8 ;
		}
		.stepBtn {
			width: 664rpx;
			height: 90rpx;
			border-radius: 45rpx;
			line-height: 90rpx;
			text-align: center;
			font-size: 30rpx;
			color: #fff;
			background: linear-gradient(to right,#fc4250, #fe744b);
		}
	}
</style>
